﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using HandBook.Data;
using Telerik.Web.UI;
using HandBook.Common;

public partial class Admin_FoodStyleManagement : System.Web.UI.Page
{
    public bool isDuplicate = false;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void uxFoodStyle_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        fillAllData();
    }

    private void fillAllData()
    {
        var foodStyle = from style in HandBookData.EatDrinkDB.FoodStyles
                        select style;
        uxFoodStyle.DataSource = foodStyle;
    }
    protected void uxFoodStyle_UpdateCommand(object source, GridCommandEventArgs e)
    {      
            GridEditableItem editedItem = e.Item as GridEditableItem;
            int foodStyleID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["FoodStyleID"]);
            var food = HandBookData.EatDrinkDB.FoodStyles.Single(f => f.FoodStyleID == foodStyleID);
            string foodStyleName = (e.Item.FindControl("txtFoodStyleName") as TextBox).Text.Trim();
            //Kiem tra xem ten Food Style co trong csdl
            var foodStyle = from s in HandBookData.EatDrinkDB.FoodStyles
                            where s.FoodStyleID != foodStyleID
                            select s;
            foreach (var item in foodStyle)
            {
                if (item.FoodStyleName.Equals(foodStyleName))
                {
                    isDuplicate = true;
                    break;
                }
            }
            if (isDuplicate)
            {
                e.Canceled = true;
                (e.Item.FindControl("lbError") as Label).Visible = true;
            }
            else
            {
                food.FoodStyleName = foodStyleName;
                HandBookData.EatDrinkDB.SubmitChanges();
            }
    }
    protected void uxFoodStyle_InsertCommand(object source, GridCommandEventArgs e)
    {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            FoodStyle foodStyle = new FoodStyle();
            foodStyle.FoodStyleName = (e.Item.FindControl("txtFoodStyleName") as TextBox).Text.Trim();
            //Kiem tra xem ten Food Style co trong csdl
            var foodStyleName = from s in HandBookData.EatDrinkDB.FoodStyles
                            select s;
            foreach (var item in foodStyleName)
            {
                if (item.FoodStyleName.Equals(foodStyle.FoodStyleName))
                {
                    isDuplicate = true;
                    break;
                }
            }
            if (isDuplicate)
            {
                e.Canceled = true;
                (e.Item.FindControl("lbError") as Label).Visible = true;
            }
            else
            {
                HandBookData.EatDrinkDB.FoodStyles.InsertOnSubmit(foodStyle);
                HandBookData.EatDrinkDB.SubmitChanges();
            }
             
    }
    protected void uxFoodStyle_CancelCommand(object source, GridCommandEventArgs e)
    {
        fillAllData();
    }
    protected void uxFoodStyle_DeleteCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        int foodStyleID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["FoodStyleID"]);
        var food = HandBookData.EatDrinkDB.FoodStyles.Single(f => f.FoodStyleID == foodStyleID);
        HandBookData.EatDrinkDB.FoodStyles.DeleteOnSubmit(food);
        HandBookData.EatDrinkDB.SubmitChanges();
    }
}
